package com.spring.mvc.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 文件操作工具类
 * @author liujian
 *
 */
public class FileUtil {
	
	static final Logger logger=LoggerFactory.getLogger(FileUtil.class);

	public static void writeFile(String fileName,String path,String context){
		if(!path.endsWith(File.separator)){
			path+=File.separator;
		}
		if(fileName.startsWith(File.separator)){
			fileName=fileName.substring(File.separator.length());
		}
		File file=new File(path);
		if(!file.exists()){
			file.mkdirs();
		}
		BufferedWriter bw=null;
		try {
			bw=new BufferedWriter(
					new OutputStreamWriter(
							new FileOutputStream(path+fileName)));
			bw.write(context);
			bw.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(null != bw){
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 根据给定路径读取文件内容
	 * @param path
	 * @return
	 */
	public static String readFile(String path){
		File file=new File(path);
		if(!file.exists() || file.isDirectory()){
			logger.error("File is Null or File is Dir");
			return null;
		}
		StringBuilder sb=new StringBuilder();
		BufferedReader br=null;
		try {
			br=new BufferedReader(
					new InputStreamReader(
							new FileInputStream(file)));
			String msg=null;
			while((msg=br.readLine())!=null){
				sb.append(msg+"</br>");
				parse(msg);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(br!=null){
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return sb.toString();
	}
	
	/**
	 * 在指定文件头部增加内容
	 * @param path 文件路径
	 * @param fileName 文件名
	 * @param content 内容
	 */
	public static void addFileHeader(String path,String fileName,String content){
		RandomAccessFile file=null;
		byte[] buf=null;
		try {
			file=new RandomAccessFile(path+fileName, "rw");
			file.seek(file.length());
			buf=new byte[4096];
			long currentRead=file.length();
			long step=content.length();
			long currentWrite=0;
			int offset=0;
			while(currentRead>0){
				if(currentRead<buf.length){
					offset=(int)currentRead;
					currentRead=0;
					currentWrite=currentRead+step;
				}else{
					currentRead-=buf.length;
					offset=buf.length;
					currentWrite=currentRead+step;
				}
				file.seek(currentRead);
				file.read(buf,0,offset);
				file.seek(currentWrite);
				file.write(buf,0,offset);
			}
			file.seek(0);
			file.writeBytes(content);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(file!=null){
				try {
					file.close();
					buf=null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	
	/**
	 * 文件复制
	 * @param fileName1
	 * @param fileName2
	 */
	public static void copyFile(String fileName1,String fileName2){
		FileInputStream fis=null;
		FileOutputStream fos=null;
		try {
			fis=new FileInputStream(fileName1);
			fos=new FileOutputStream(fileName2);
			
			FileChannel fcin=fis.getChannel();
			FileChannel fcout=fos.getChannel();
			
			ByteBuffer buffer=ByteBuffer.allocate(1024);
			while(true){
				//重设缓冲区，使它可以接受读入的数据
				buffer.clear();
				int r=fcin.read(buffer);
				if(r==-1){
					break;
				}
				buffer.flip();
				fcout.write(buffer);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				if(null != fis){
					fis.close();
				}
				if(null != fos){
					fos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private static int sum=0;
	public static void parse(String msg) {
		if(msg.indexOf("WEBSVRCODE=\"A00005\"")!=-1){//A00004 58 A00003 145 A00005 76 A00001 52 D00001
			System.out.println(msg);
			sum++;
		}
	}
	
	public static void main(String[] args) {
		
		//FileUtil.copyFile("D:\\test.txt", "D:\\2.sql");
		FileUtil.readFile("E:\\doc\\监控\\监控\\20121224-2\\AA0012.txt");
		System.out.println(sum);
	/*	long startTime=System.currentTimeMillis();
//		for(int i=0;i<1000;i++){
			addFileHeader("E:\\doc\\监控\\监控\\20121203-1\\","AA0010.txt","hello world\r\n");
		//}
		long endTime=System.currentTimeMillis();
		System.out.println(endTime-startTime);*/
	}
	
}
